using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using WP.Commons;
using WP.DB;
using WP.Modules;
using WP.Controls;

namespace WP.MF.Form
{

    //Function : Update Priod   
    //Input :    gCurrentMenuInfo.Parameter=1 ,  GL
    //           gCurrentMenuInfo.Parameter=2 ,  AP
    //           gCurrentMenuInfo.Parameter=3 ,  AR
    //           gCurrentMenuInfo.Parameter=4 ,  OE
    //           gCurrentMenuInfo.Parameter=5 ,  IM
    //           gCurrentMenuInfo.Parameter=6 ,  PO
    //           gCurrentMenuInfo.Parameter=7 ,  RM
    //           gCurrentMenuInfo.Parameter=8 ,  M

    public partial class frmMPeriodMtn : WP.Modules.BaseQryRpt
    {

        #region Define Variable
        private string m_strModuleName = "";
        private SystemModules m_smModule = SystemModules.AP;
        private bool m_bUpdatePeriod = false;
        private bool m_b445Periods = false;
        private int m_iCurPrd = 0;
        private string m_OrigPeriod = string.Empty;
        private DateTime m_DatStart;
        private DateTimeEx[] m_ArrUdtStPrdDate = new DateTimeEx[24];
        private DateTimeEx[] m_ArrUdtEnPrdDate = new DateTimeEx[24];
        private DateTimeEx[] m_ArrUdtPreYrStPrdDate = new DateTimeEx[12];
        private DateTimeEx[] m_ArrUdtPreYrEnPrdDate = new DateTimeEx[12];
        private DateTimeEx[] m_ArrUdtPrePreYrStPrdDate = new DateTimeEx[12];
        private DateTimeEx[] m_ArrUdtPrePreYrEnPrdDate = new DateTimeEx[12];

        #endregion

        #region Constructor Functions
        public frmMPeriodMtn()
        {
            InitializeComponent();
            LoadAllControl(new ArrayList(this.Controls));
            this.arraycontrols.Sort(new mySortControlByName());
            this.MinimumSize = this.Size;
        }
        #endregion

        #region ***Form Events

        private void frmMPeriodMtn_Load(object sender, System.EventArgs e)
        {
            InitArray();
            
            m_bUpdatePeriod = DataFunctions.GetmSwitchValue("UpdatePeriod", "M");
            m_b445Periods = DataFunctions.GetmSwitchValue("445Periods", "M");

            #region set control status
            if (!m_bUpdatePeriod)
            {
                btnUpdate.Enabled = false;
                //dtpUpdate.Enabled = false
                udtSelect.Enabled = false;
                txtCurPrd.Enabled = false;
            }
            else if (m_bUpdatePeriod && !m_b445Periods)
            {
                //dtpUpdate.Enabled = False
            }
            else if (m_bUpdatePeriod && m_b445Periods)
            {
                udtSelect.Enabled = false;
            }
            if (!m_b445Periods)
            {
                btnCopy.Visible = false;
                copyToolStripMenuItem.Visible = false;
                LockControl(DateTimeEx.UserStyle.CanNotAll);
            }
            else
                LockControl(DateTimeEx.UserStyle.CanWriteSelect);
            #endregion

            #region set ModuleName
            switch (CurrentMenuInfo.iParameter)
            {
                case 1:
                    m_strModuleName = "GL";
                    m_smModule = SystemModules.GL;
                    SetGLPeriodFace();
                    break;
                case 2:
                    m_strModuleName = "AP";
                    m_smModule = SystemModules.AP;
                    break;
                case 3:
                    m_strModuleName = "AR";
                    m_smModule = SystemModules.AR;
                    break;
                case 4:
                    m_strModuleName = "OE";
                    m_smModule = SystemModules.OE;
                    break;
                case 5:
                    m_strModuleName = "IM";
                    m_smModule = SystemModules.IM;
                    break;
                case 6:
                    m_strModuleName = "PO";
                    m_smModule = SystemModules.PO;
                    break;
                case 7:
                    m_strModuleName = "RM";
                    m_smModule = SystemModules.RM;
                    break;
                case 8:
                    m_strModuleName = "M";
                    m_smModule = SystemModules.MF;
                    break;
                default:
                    break;
            }
            #endregion

            InitControlValue();
            DisplayCurPrdColor();
        }
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (!UpdateData()) return;
            this.Close();
        }
        private void btnCopy_Click(object sender, EventArgs e)
        {
            string strResult = string.Empty;
            string strSql = string.Empty;
            string strToModules = string.Empty;
            string strToModuleName = string.Empty;
            string[] strTo;
            int i = 0;
            bool bResult = false;
            bool bOeImPoRmModul = false;
            DBAccess dBAccess = new DBAccess(false,true);
                
            if (!UpdateData()) return;
            
            strToModules = "GL,AP,AR,OE,IM,PO,RM,M";
            
            strToModules = strToModules.Replace(m_strModuleName, "");
            strToModules = strToModules.Replace(",,", ",");
            
            frmInput FrmInput = new frmInput();
            strResult = FrmInput.InputData("From " + m_strModuleName + " to " + strToModules + " " + "\r\n" + "If copy to multi modules, Please use " + "\"" + "," + "\"" + " to split them.", "Copy Period", "", "", "", "", true);
            
            if (strResult.Trim() == string.Empty)
                return;

            if (strResult.Trim().ToUpper() == m_strModuleName.Trim().ToUpper())
            {
                MsgBoxEx.Show(DataFunctions.GetMsg(this.Name, "MSG_CopyToModule"), SysVar.WarnTitle);
                return;
            }
            
            strTo = strResult.Split(',');

            if (strResult == "%NONE%") return;

            for (i = 0; i < strTo.Length; i++)
            {
                if (strToModules.IndexOf(strTo[i].Trim()) < 0)
                {
                    MsgBoxEx.Show("The module name must be " + strToModules, SysVar.WarnTitle);
                    return;
                }
            }

            strToModules = "";
            try
            {
                for (i = 0; i< strTo.Length; i++)
                {
                    strToModuleName = strTo[i].Trim().ToUpper();
                    
                    if (strToModuleName != m_strModuleName.Trim().ToUpper())
                    {
                        if (strToModuleName == "AP" || strToModuleName == "AR" ||
                            strToModuleName == "GL" || strToModuleName == "IM" || 
                            strToModuleName == "M"  || strToModuleName == "OE" || 
                            strToModuleName == "PO" || strToModuleName == "RM")
                        {
                            strSql = "Update mperiod set CurPrd=m.CurPrd,StPrdDate=m.StPrdDate,EnPrdDate=m.EnPrdDate,PreYrStPrdDate=m.PreYrStPrdDate,PreYrEnPrdDate=m.PreYrEnPrdDate,PrePreYrStPrdDate=m.PrePreYrStPrdDate,PrePreYrEnPrdDate=m.PrePreYrEnPrdDate";
                            strSql+= " from (select CurPrd,Period,StPrdDate,EnPrdDate,PreYrStPrdDate,PreYrEnPrdDate,PrePreYrStPrdDate,PrePreYrEnPrdDate from mperiod where ModuleName='" + m_strModuleName + "') m ";
                            strSql+= " where mperiod.Period=m.Period ";
                            strSql+= " and mperiod.ModuleName='" + strToModuleName + "'";
                            
                            dBAccess.ExecuteSql(strSql);
                            bResult = true;
                            
                            strToModules = (strToModules == ""? strToModuleName: strToModules + "," + strToModuleName);
                            
                            if (BaseFunctions.InStrEx("PO,OE,RM,IM", strToModuleName.Trim(), ","))
                                bOeImPoRmModul = true;
                        }
                    }
                }
                
                if (bResult)
                {
                    if (bOeImPoRmModul)
                    {
                        strSql = "Update mperiod set curPrd=" + CvtFunc.ToInt(txtCurPrd.Text) + " where Period='CP' and (ModuleName='IM' or ModuleName='RM' or ModuleName='PO' or ModuleName='OE')";
                        dBAccess.ExecuteSql(strSql);
                        
                        strSql = "Update mperiod set StPrdDate=m.StPrdDate,EnPrdDate=m.EnPrdDate,PreYrStPrdDate=m.PreYrStPrdDate,PreYrEnPrdDate=m.PreYrEnPrdDate,PrePreYrStPrdDate=m.PrePreYrStPrdDate,PrePreYrEnPrdDate=m.PrePreYrEnPrdDate";
                        strSql+= " from (select Period,StPrdDate,EnPrdDate,PreYrStPrdDate,PreYrEnPrdDate,PrePreYrStPrdDate,PrePreYrEnPrdDate from mperiod where ModuleName='" + m_strModuleName + "' and Period<>'CP') m ";
                        strSql+= " where mperiod.Period=m.Period ";
                        strSql+= " and (mperiod.ModuleName='OE' or mperiod.ModuleName='PO' or mperiod.ModuleName='IM' or mperiod.ModuleName='RM')";
                        dBAccess.ExecuteSql(strSql);
                    }
                    MsgBoxEx.Show(DataFunctions.GetMsg(this.Name, "MSG_OkFor", new string[] { strToModules }), SysVar.WarnTitle);
                }

                dBAccess.CommitTranAndCloseCon();
            }
            catch(Exception ex)
            {
                dBAccess.RollbackTranAndClose();
                MsgBoxEx.Show(ex.Message,SysVar.WarnTitle);
            }
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void txtCurPrd_TextChanged(object sender, EventArgs e)
        {
            DisplayCurPrdColor();
        }
        private void udt_Leave(object sender, EventArgs e)
        {
            DateTimeEx udtRelate = null;
            DateTimeEx udtCur = sender as DateTimeEx;
            int iConLenth = 0;
            int iNumLenth = 0;
            int i = 0;
            string strRelateName = string.Empty;

            if (udtCur.Name == udtPrePreYrStPrdDate0.Name)
                return;

            if (udtCur.Name == udtEnPrdDate12.Name)
                return;

            #region get Start Relate Control
            if (udtCur.Name.IndexOf("St", StringComparison.CurrentCulture) != -1)
            {
                if (udtCur.Name ==  udtPreYrStPrdDate0.Name)
                {
                    udtRelate = udtPrePreYrEnPrdDate11;
                }
                else if (udtCur.Name == udtStPrdDate0.Name)
                {
                    udtRelate = udtPreYrEnPrdDate11;
                }
                else
                {
                    if (udtCur.Parent == gpbPrePreYr)
                    {
                        strRelateName = "udtPrePreYrEnPrdDate";
                        iConLenth = 21; //"udtPrePreYrStPrdDate" or "udtPrePreYrEnPrdDate"
                    }
                    else if (udtCur.Parent == gpbPreYr)
                    {
                        strRelateName = "udtPreYrEnPrdDate";
                        iConLenth = 18; //"udtPreYrStPrdDate" or "udtPreYrEnPrdDate"
                    }
                    else if (udtCur.Parent == gpbCurYr)
                    {
                        strRelateName = "udtEnPrdDate";
                        iConLenth = 13; //"udtStPrdDate" or "udtEnPrdDate"
                    }

                    if (udtCur.Name.Length == iConLenth)
                        iNumLenth = 1;
                    else
                        iNumLenth = 2;

                    i = CvtFunc.ToInt(udtCur.Name.Substring(iConLenth - iNumLenth)) - 1;
                    strRelateName = strRelateName + i.ToString();
                    udtRelate = (DateTimeEx)GetControl(strRelateName);
                }
                if (udtRelate != null)
                    udtRelate.Text = BaseFunctions.FormatDate(CvtFunc.ToDateTime(udtCur.Text).AddDays(-1));

                return;
            }
            #endregion

            #region get End Relate Control
            if (udtCur.Name.IndexOf("En", StringComparison.CurrentCulture) != -1)
            {
                if (udtCur.Name ==  udtPrePreYrEnPrdDate11.Name)
                {
                    udtRelate = udtPreYrStPrdDate0;
                }
                else if (udtCur.Name == udtPreYrEnPrdDate11.Name)
                {
                    udtRelate = udtStPrdDate0;
                }
                else
                {
                    if (udtCur.Parent == gpbPrePreYr)
                    {
                        strRelateName = "udtPrePreYrStPrdDate";
                        iConLenth = 21; //"udtPrePreYrStPrdDate" or "udtPrePreYrEnPrdDate"
                    }
                    else if  (udtCur.Parent == gpbPreYr)
                    {
                        strRelateName = "udtPreYrStPrdDate";
                        iConLenth = 18; //"udtPreYrStPrdDate" or "udtPreYrEnPrdDate"
                    }
                    else if  (udtCur.Parent == gpbCurYr)
                    {
                        strRelateName = "udtStPrdDate";
                        iConLenth = 13; //"udtStPrdDate" or "udtEnPrdDate"
                    }

                    if (udtCur.Name.Length == iConLenth)
                        iNumLenth = 1;
                    else
                        iNumLenth = 2;

                    i = CvtFunc.ToInt(udtCur.Name.Substring(iConLenth - iNumLenth)) + 1;
                    strRelateName = strRelateName + i.ToString();
                    udtRelate = (DateTimeEx)GetControl(strRelateName);
                }
                if (udtRelate != null)
                    udtRelate.Text = BaseFunctions.FormatDate(CvtFunc.ToDateTime(udtCur.Text).AddDays(1));

                return;
            }
            #endregion
        }

        #endregion Form Events***

        #region ***Form Functions
        private void InitArray()
        {
            int i = 0;
            int iNumLenth = 0;
            
            foreach (Control con in gpbCurYr.Controls)
            {
                if (con.GetType().Name.ToUpper() == "DATETIMEEX")
                {
                    if (con.Name.Length == 13) //"udtStPrdDate" or "udtEnPrdDate"
                        iNumLenth = 1;
                    else
                        iNumLenth = 2;

                    i = CvtFunc.ToInt(con.Name.Substring(con.Name.Length - iNumLenth));
                    if (con.Name.Substring(0, con.Name.Length - iNumLenth) == "udtStPrdDate")
                    {
                        m_ArrUdtStPrdDate[i] = con as DateTimeEx;
                    }
                    else if (con.Name.Substring(0, con.Name.Length - iNumLenth) == "udtEnPrdDate")
                    {
                        m_ArrUdtEnPrdDate[i] = con as DateTimeEx;
                    }
                    con.Leave += new EventHandler(udt_Leave);
                }
            }
            i = 0;
            foreach (Control con in gpbPreYr.Controls)
            {
                if (con.GetType().Name.ToUpper() == "DATETIMEEX")
                {
                    if (con.Name.Length == 18) //"udtPreYrStPrdDate" or "udtPreYrEnPrdDate"
                        iNumLenth = 1;
                    else
                        iNumLenth = 2;

                    i = CvtFunc.ToInt(con.Name.Substring(con.Name.Length - iNumLenth));
                    if (con.Name.Substring(0, con.Name.Length - iNumLenth) == "udtPreYrStPrdDate")
                    {
                        m_ArrUdtPreYrStPrdDate[i] = con as DateTimeEx;
                    }
                    else if (con.Name.Substring(0, con.Name.Length - iNumLenth) == "udtPreYrEnPrdDate")
                    {
                        m_ArrUdtPreYrEnPrdDate[i] = con as DateTimeEx;
                    }
                    con.Leave += new EventHandler(udt_Leave);
                }
            }
            i = 0;
            foreach (Control con in gpbPrePreYr.Controls)
            {
                if (con.GetType().Name.ToUpper() == "DATETIMEEX")
                {
                    if (con.Name.Length == 21) //"udtPrePreYrStPrdDate" or "udtPrePreYrEnPrdDate"
                        iNumLenth = 1;
                    else
                        iNumLenth = 2;

                    i = CvtFunc.ToInt(con.Name.Substring(con.Name.Length - iNumLenth));
                    if (con.Name.Substring(0, con.Name.Length - iNumLenth) == "udtPrePreYrStPrdDate")
                    {
                        m_ArrUdtPrePreYrStPrdDate[i] = con as DateTimeEx;
                    }
                    else if (con.Name.Substring(0, con.Name.Length - iNumLenth) == "udtPrePreYrEnPrdDate")
                    {
                        m_ArrUdtPrePreYrEnPrdDate[i] = con as DateTimeEx;
                    }
                    con.Leave += new EventHandler(udt_Leave);
                }
            }
        }

        private void InitControlValue()
        {
            int i=0;
            int intMaxPrd=0;
            string strSql="";
            const int GLMaxPrd = 24;
            const int MaxPrd = 13;
            DBAccess dBAccess = new DBAccess(false,false);
            DataTable dtPeriod = null;
            DataRow drPeriod = null;

            txtCurPrd.Text = DataFunctions.GetPrdInfo(m_smModule, PeriodRange.PeriodCur, PeriodOption.Period);
            this.m_OrigPeriod = txtCurPrd.Text;
            m_iCurPrd = CvtFunc.ToInt(txtCurPrd.Text);

            strSql = "SELECT Period, StPrdDate, EnPrdDate, PreYrStPrdDate, PreYrEnPrdDate, PrePreYrStPrdDate, ";
            strSql += "PrePreYrEnPrdDate FROM mPeriod";
            strSql += " WHERE ModuleName = '" + m_strModuleName + "' AND Period <> 'CP'";
            strSql += " ORDER BY Period";
            dtPeriod = dBAccess.GetDataTable(strSql);

            if (dtPeriod.Rows.Count == 0)
            {
                dBAccess.CloseCon();
                return;
            }

            intMaxPrd = (m_smModule == SystemModules.GL? GLMaxPrd: MaxPrd);

            for (i = 0; i<=intMaxPrd - 1; i++)
            {
                drPeriod = dtPeriod.Rows[i];
                m_ArrUdtStPrdDate[i].Text = BaseFunctions.FormatDate(drPeriod["StPrdDate"].ToString());
                m_ArrUdtEnPrdDate[i].Text = BaseFunctions.FormatDate(drPeriod["EnPrdDate"].ToString());
                if (i < 12)
                {
                    m_ArrUdtPreYrStPrdDate[i].Text = BaseFunctions.FormatDate(drPeriod["PreYrStPrdDate"].ToString());
                    m_ArrUdtPreYrEnPrdDate[i].Text = BaseFunctions.FormatDate(drPeriod["PreYrEnPrdDate"].ToString());
                    m_ArrUdtPrePreYrStPrdDate[i].Text = BaseFunctions.FormatDate(drPeriod["PrePreYrStPrdDate"].ToString());
                    m_ArrUdtPrePreYrEnPrdDate[i].Text = BaseFunctions.FormatDate(drPeriod["PrePreYrEnPrdDate"].ToString());
                }
            }

            udtSelect.Text = udtStPrdDate0.Text;
            m_DatStart = CvtFunc.ToDateTime(udtSelect.Text);
            
            dBAccess.CloseCon();
        }

        private void DisplayCurPrdColor()
        {
           int iCurPrd=0, i=0;

           iCurPrd = CvtFunc.ToInt(BaseFunctions.FormatNumeric(txtCurPrd.Text, "n.0"));
           if (iCurPrd > 0 && iCurPrd < 13)
            {
                m_ArrUdtStPrdDate[iCurPrd - 1].ForeColor = Color.SeaGreen; //Color.LawnGreen;DarkBlue
                m_ArrUdtEnPrdDate[iCurPrd - 1].ForeColor = Color.SeaGreen;
                for (i = 0; i <= 12; i++)
                {
                    if (i != iCurPrd - 1)
                    {
                        m_ArrUdtStPrdDate[i].ForeColor = Color.Black;
                        m_ArrUdtEnPrdDate[i].ForeColor = Color.Black;
                    }
                }
            }
        }

        private void SetGLPeriodFace()
        {
            int i = 0;

            if (m_strModuleName != "GL") return;

            gpbCurYr.Width = 400;//338;
            gpbCurYr.Height = gpbPreYr.Height;
            gpbPreYr.Left = gpbPreYr.Left + 204;
            gpbPrePreYr.Left = gpbPrePreYr.Left + 204;
            //pnlDate.Left = pnlDate.Left + 204;
            //this.Width = this.Width + 204;
            lblPeriod12.Visible = false;
            udtStPrdDate12.Left = udtStPrdDate13.Left;
            udtStPrdDate12.Top  = udtStPrdDate0.Top;
            udtEnPrdDate12.Left = udtEnPrdDate13.Left;
            udtEnPrdDate12.Top  = udtEnPrdDate0.Top;
            
            for (i = 13; i<=23; i++)
            {
                m_ArrUdtStPrdDate[i].Visible = true;
                m_ArrUdtEnPrdDate[i].Visible = true;
            }
        }

        private bool UpdateData()
        {
            try
            {
                //if (m_smModule == SystemModules.OE || m_smModule == SystemModules.PO || m_smModule == SystemModules.IM || m_smModule == SystemModules.RM)
                //{
                //    if (DataFunctions.GetmSwitchValue("MustProcessInvMthEnding","IM"))
                //    {
                //        string strProd = CvtFunc.ToString(CvtFunc.ToInt(DataFunctions.GetPrdInfo(SystemModules.IM, PeriodRange.PeriodCur, PeriodOption.Period)));
                //        int iPrdYear = CvtFunc.ToDateTime(BaseFunctions.FormatDate(DataFunctions.GetPrdInfo(SystemModules.IM, PeriodRange.Period01, PeriodOption.StPrdDate))).Year;
                //        if (!DataFunctions.CheckRecordExist("imMonthEndInvValue", iPrdYear.ToString(), "PrdYear", "Period=" + strProd))
                //        {
                //            MsgBoxEx.Show("You must do Inventory Month-End Processing.", SysVar.WarnTitle);
                //            return false;
                //        }
                //    }
                //}
                if (m_b445Periods)
                {
                    if (!ValidCheck()) return false;
                    if (!UpdateTable(m_strModuleName)) return false;
                }
                else
                    if (!DataFunctions.UpdatePeriod(null, m_strModuleName, CvtFunc.ToDateTime(udtSelect.Text), CvtFunc.ToInt(txtCurPrd.Text)))
                        return false;
                    else
                    {
                        #region Add log
                        DBAccess dBAccess = new DBAccess(false, false);
                        string strlog = string.Empty;
                        if (m_OrigPeriod != txtCurPrd.Text)
                        {
                            strlog = "Change current period from " + m_OrigPeriod + " to " + txtCurPrd.Text + "";
                            AddLogFile(dBAccess, strlog);
                        }
                        
                        if (BaseFunctions.FormatDate(m_DatStart) != BaseFunctions.FormatDate(udtSelect.Value))
                        {
                            strlog = "Change starting date from " + BaseFunctions.FormatDate(m_DatStart) + " to " + BaseFunctions.FormatDate(udtSelect.Value);
                            AddLogFile(dBAccess, strlog);
                        }
                        dBAccess.CloseCon();
                        #endregion
                    }

                InitControlValue();
            }
            catch(Exception ex)
            {
                MsgBoxEx.Show(ex.Message,SysVar.WarnTitle);
                return false;
            }
            return true;
        }

        private void AddLogFile(DBAccess dBAccess, string strlog)
        {
            switch (m_smModule)
            {
                case SystemModules.OE:
                case SystemModules.IM:
                case SystemModules.PO:
                case SystemModules.RM:
                    DataFunctions.AddLog(dBAccess, strlog, "OE", "");
                    DataFunctions.AddLog(dBAccess, strlog, "RM", "");
                    DataFunctions.AddLog(dBAccess, strlog, "PO", "");
                    DataFunctions.AddLog(dBAccess, strlog, "IM", "");
                    break;
                default:
                    DataFunctions.AddLog(dBAccess, strlog, m_strModuleName, "");
                    break;
            }
        }

        private bool ValidCheck()
        {
            int i = 0;
            DBAccess dbAccess = new DBAccess(false,false); 
            for (i = 0; i< m_ArrUdtPrePreYrStPrdDate.Length; i++)
            {
                if (CvtFunc.ToDateTime(m_ArrUdtPrePreYrStPrdDate[i].Text) > CvtFunc.ToDateTime(m_ArrUdtPrePreYrEnPrdDate[i].Text))
                {
                    MsgBoxEx.Show(DataFunctions.GetMsg("DateGreater"), SysVar.WarnTitle);
                    m_ArrUdtPrePreYrStPrdDate[i].Focus();
                    return false;
                }
            }

            for (i = 0; i < m_ArrUdtPreYrStPrdDate.Length; i++)
            {
                if (CvtFunc.ToDateTime(m_ArrUdtPreYrStPrdDate[i].Text) > CvtFunc.ToDateTime(m_ArrUdtPreYrEnPrdDate[i].Text))
                {
                    MsgBoxEx.Show(DataFunctions.GetMsg("DateGreater"), SysVar.WarnTitle);
                    m_ArrUdtPreYrStPrdDate[i].Focus();
                    return false;
                }
            }
            
            for (i = 0; i< m_ArrUdtStPrdDate.Length; i++)
            {
                if (m_ArrUdtStPrdDate[i].Text.Trim() != "" && m_ArrUdtStPrdDate[i].Text.Trim() != "/  /")
                {
                    if (CvtFunc.ToDateTime(m_ArrUdtStPrdDate[i].Text) > CvtFunc.ToDateTime(m_ArrUdtEnPrdDate[i].Text))
                    {
                        MsgBoxEx.Show(DataFunctions.GetMsg("DateGreater"), SysVar.WarnTitle);
                        m_ArrUdtStPrdDate[i].Focus();
                        return false;
                    }
                }
            }
            dbAccess.CloseCon();
            return true;
        }

        private bool UpdateTable(string strModuleName)
        {
            int i = 0;
            int iEnd = 0;
            string strSql = string.Empty;
            string strFields = string.Empty;
            DBAccess dBAccess = new DBAccess(false, true);
            DataView dvPrd=null;

            try
            {
                strSql = "select RecID,Period,ModuleName,StPrdDate,EnPrdDate,PreYrStPrdDate,PreYrEnPrdDate,";
                strSql+= "PrePreYrStPrdDate,PrePreYrEnPrdDate,CurPrd from mPeriod ";
                strSql+= "where ModuleName='" + strModuleName + "'";

         
                dvPrd = dBAccess.GetDataTable(strSql).DefaultView;

                strFields = "Period,ModuleName,StPrdDate,EnPrdDate,PreYrStPrdDate,PreYrEnPrdDate,PrePreYrStPrdDate,PrePreYrEnPrdDate,CurPrd";
                dvPrd.RowFilter = "Period='CP'";
                if (dvPrd.Count == 0)   //No find , then add
                {
                    dvPrd.AddNew();
                    dvPrd[0]["ModuleName"] = strModuleName;
                    dvPrd[0]["Period"] = "CP";
                }
                dvPrd[0]["curPrd"] = CvtFunc.ToInt(txtCurPrd.Text);
                DataFunctions.UpdateTable(dBAccess, strFields, "mPeriod", dvPrd[0].Row, (dvPrd[0].IsNew == true ? FormStatus.AddNew : FormStatus.Update), "RecID", false);

                iEnd = (strModuleName == "GL"? 24: 13);
                for (i = 1; i<=iEnd; i++)
                {
                    dvPrd.RowFilter = "Period='" + string.Format("{0:00}", i) + "' or Period='" + i.ToString() + "'";
                    if (CvtFunc.ToInt(dBAccess.GetValue("select count(*) from mPeriod where (Period='" + string.Format("{0:00}", i) + "' or Period='" + i.ToString() + "')")) == 0)   //No find , add
                    {
                        dvPrd.AddNew();
                        dvPrd[0]["ModuleName"] = strModuleName;
                        dvPrd[0]["Period"] = string.Format("{0:00}",i);
                    }
                
                    dvPrd[0]["StPrdDate"] = CvtFunc.ToDateTime(m_ArrUdtStPrdDate[i - 1].Text);
                    dvPrd[0]["EnPrdDate"] = CvtFunc.ToDateTime(m_ArrUdtEnPrdDate[i - 1].Text);

                    if (i <= 12)
                    {
                        dvPrd[0]["PreYrStPrdDate"]    = CvtFunc.ToDateTime(m_ArrUdtPreYrStPrdDate[i - 1].Text);
                        dvPrd[0]["PreYrEnPrdDate"]    = CvtFunc.ToDateTime(m_ArrUdtPreYrEnPrdDate[i - 1].Text);
                        dvPrd[0]["PrePreYrStPrdDate"] = CvtFunc.ToDateTime(m_ArrUdtPrePreYrStPrdDate[i - 1].Text);
                        dvPrd[0]["PrePreYrEnPrdDate"] = CvtFunc.ToDateTime(m_ArrUdtPrePreYrEnPrdDate[i - 1].Text);
                    }
                    else
                    {
                        dvPrd[0]["PreYrStPrdDate"]    = CvtFunc.ToDateTime(m_ArrUdtStPrdDate[i - 13].Text);
                        dvPrd[0]["PreYrEnPrdDate"]    = CvtFunc.ToDateTime(m_ArrUdtEnPrdDate[i - 13].Text);
                        dvPrd[0]["PrePreYrStPrdDate"] = CvtFunc.ToDateTime(m_ArrUdtPreYrStPrdDate[i - 13].Text);
                        dvPrd[0]["PrePreYrEnPrdDate"] = CvtFunc.ToDateTime(m_ArrUdtPreYrEnPrdDate[i - 13].Text);
                    }
                    DataFunctions.UpdateTable(dBAccess, strFields, "mPeriod", dvPrd[0].Row, (dvPrd[0].IsNew == true ? FormStatus.AddNew : FormStatus.Update), "RecID", false);
                }
                
                //if is PO/OE/RM/IM current period  occurs changed, then changed it to SAME
                if (BaseFunctions.InStrEx("PO/OE/RM/IM", strModuleName.Trim(), "/"))
                {
                    strSql = "Update mperiod set curPrd=" + CvtFunc.ToInt(txtCurPrd.Text) + " where Period='CP' and (ModuleName='IM' or ModuleName='RM' or ModuleName='PO' or ModuleName='OE')";
                    dBAccess.ExecuteSql(strSql);
                    
                    strSql = "Update mperiod set StPrdDate=m.StPrdDate,EnPrdDate=m.EnPrdDate,PreYrStPrdDate=m.PreYrStPrdDate,PreYrEnPrdDate=m.PreYrEnPrdDate,PrePreYrStPrdDate=m.PrePreYrStPrdDate,PrePreYrEnPrdDate=m.PrePreYrEnPrdDate";
                    strSql+= " from (select Period,StPrdDate,EnPrdDate,PreYrStPrdDate,PreYrEnPrdDate,PrePreYrStPrdDate,PrePreYrEnPrdDate from mperiod where ModuleName='" + strModuleName + "' and Period<>'CP') m ";
                    strSql+= " where mperiod.Period=m.Period ";
                    strSql+= " and (mperiod.ModuleName='OE' or mperiod.ModuleName='PO' or mperiod.ModuleName='IM' or mperiod.ModuleName='RM') and mperiod.ModuleName<>'" + strModuleName + "'";
                    dBAccess.ExecuteSql(strSql);
                }
                
                dBAccess.CommitTranAndCloseCon();
                return true;
            }
            catch(Exception ex)
            {
                dBAccess.RollbackTranAndClose();
                throw new Exception(ex.Message);
                return false;
            }
        }

        private void LockControl(DateTimeEx.UserStyle LsLock)
        {
            DateTimeEx udt = null;

            foreach (Control con in gpbCurYr.Controls)
            {
                udt = con as DateTimeEx;
                if (udt != null)
                    udt.LockStyle = LsLock;
            }

            foreach (Control con in gpbPreYr.Controls)
            {
                udt = con as DateTimeEx;
                if (udt != null)
                    udt.LockStyle = LsLock;
            }

            foreach (Control con in gpbPrePreYr.Controls)
            {
                udt = con as DateTimeEx;
                if (udt != null)
                    udt.LockStyle = LsLock;
            }
        }
        #endregion Form Functions***

    }
}